package C059;

import C017.main;

import java.util.Arrays;
import java.util.Scanner;

/**
 * ClassName: c02_01
 * Package: C059
 * Description:  【模板】拓扑排序  牛客网https://www.nowcoder.com/practice/88f7e156ca7d43a1a535f619cd3f495c
 * 前式链向星静态模版
 * @Author BCXJ
 * @Create 2025/10/8 22:32
 * @Version 1.0
 * @Since 1.0
 */
public class c02_01 {

    /*
    5 4
    1 2
    2 3
    3 4
    4 5

    1 2 3 4 5
     */

    static int MAX_LEN = 200001;
    // 链式前向星 - 静态写法
    static int[] head = new int[MAX_LEN];
    static int[] next = new int[MAX_LEN];
    static int[] tos = new int[MAX_LEN];
    static int cnt = 0; // 边数

    // 队列
    static int[] queue = new int[MAX_LEN];
    static int l = 0, r = 0;

    // 入度表
    static int[] indegree = new int[MAX_LEN];

    // 点数和边数
    static int n = 0, m = 0;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        int[][] arr = new int[m][2];
        for (int i = 0; i < m; i++) {
            arr[i][0] = scanner.nextInt();
            arr[i][1] = scanner.nextInt();
        }
        int[] res = findOrder(arr);
        if(res == null) {
            System.out.println(-1);
        } else {
            for (int i = 0; i < res.length - 1; i++) {
                System.out.print(res[i] + " ");
            }
            System.out.print(res[res.length - 1]);
        }
    }

    private static int[] findOrder(int[][] arr) {
        // 初始化
        Arrays.fill(head, 0);
        Arrays.fill(next, 0);
        Arrays.fill(tos, 0);
        Arrays.fill(queue, 0);
        Arrays.fill(indegree, 0);
        l = r = 0;
        // 建图
        for (int i = 0; i < arr.length; i++) {
            addEdge(arr[i][0], arr[i][1]);
        }
        // 入度统计进队列
        for (int i = 1; i <= n; i++) {
            if(indegree[i] == 0) {
                queue[r ++] = i;
            }
        }
        // 计算结果
        int[] res = new int[n];
        int count = 0;
        while(l < r) {
            int curNode = queue[l ++];
            res[count ++] = curNode;
            // 遍历临边更新入度表
            int curEdge = head[curNode];
            do {
                indegree[tos[curEdge]] --;
                if(indegree[tos[curEdge]] == 0) {
                    queue[r ++] = tos[curEdge];
                }
                curEdge = next[curEdge];
            }while(curEdge != 0);
        }
        return count != n ? null : res;
    }

    // 加边
    private static void addEdge(int from, int to) {
        next[cnt] = head[from];
        head[from] = cnt;
        tos[cnt ++] = to;
        // 入度表
        indegree[to] ++;
    }


}
